package cn.maszy.service.impl.projectmanagement;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.maszy.common.Pager;
import cn.maszy.common.SessionFactory;
import cn.maszy.dao.projectmanagement.C_T_Sys_FileMapper;
import cn.maszy.model.projectmanagement.C_T_Sys_File;
import cn.maszy.model.projectmanagement.C_T_Sys_FileExample;
import cn.maszy.model.projectmanagement.C_T_Sys_FileExample.Criteria;
import cn.maszy.service.projectmanagement.C_T_Sys_FileService;
@Service
public class C_T_Sys_FileServiceImpl implements C_T_Sys_FileService {

	@Autowired
	private C_T_Sys_FileMapper c_T_Sys_FileMapper;
	
	public int update(C_T_Sys_File c_T_Sys_File) {
		// TODO Auto-generated method stub
		 if(c_T_Sys_File.getFileId() != null || !"".equals(c_T_Sys_File.getFileId())){
			 c_T_Sys_File.setIsDelete(0);
			 c_T_Sys_File.setCreateTime(new Date());
			 c_T_Sys_File.setCreateUser(SessionFactory.getSessionUserRealNam());
			 return c_T_Sys_FileMapper.insert(c_T_Sys_File);
		 }
		 else
		 {
			 c_T_Sys_File.setUpdateTime(new Date());
			 c_T_Sys_File.setUpdateUser(SessionFactory.getSessionUserRealNam());
			 return c_T_Sys_FileMapper.updateByPrimaryKeySelective(c_T_Sys_File);
		 }
	}

	public C_T_Sys_File selectOne(String staffId) {
		// TODO Auto-generated method stub
		C_T_Sys_File record=c_T_Sys_FileMapper.selectByPrimaryKey(staffId);
		if(staffId == null){
			record = new C_T_Sys_File();
		}
		return record;
	}

	public Pager<C_T_Sys_File> selectList(C_T_Sys_File c_T_Sys_File,Pager<C_T_Sys_File> pager,String filetypeId,String moduleName,String projectUuid) {
		// TODO Auto-generated method stub
		// 添加查询条件
			C_T_Sys_FileExample example=new C_T_Sys_FileExample();
				Criteria criteria = example.createCriteria();
				if(c_T_Sys_File!=null){
				if(c_T_Sys_File.getFileName()!=null && !"".equals(c_T_Sys_File.getFileName())){
						criteria.andFileNameLike("%"+c_T_Sys_File.getFileName()+"%");
				} 
			}
			if(!"".equals(moduleName)){
				criteria.andModuleNameEqualTo(moduleName);
			}
			if(!"".equals(filetypeId)){
				criteria.andTypeIdEqualTo(filetypeId);
			}
			if(!"".equals(projectUuid)){
				criteria.andUuidEqualTo(projectUuid);
			}
			criteria.andIsDeleteEqualTo(0);
			example.or(criteria);
			int totalCount=new C_T_Sys_FileServiceImpl().count(example,c_T_Sys_FileMapper);
			pager.setTotalRecords(totalCount);
			example.setLimitStart(pager.getStartRecord());
			example.setLimitEnd(pager.getPageSize());
			example.setOrderByClause(pager.getOrderField()+" "+pager.getOrderDirection());
			// 取得总的记录条数
			List<C_T_Sys_File>  list=c_T_Sys_FileMapper.selectByExample(example);
			pager.setPageRecords(list);
			return pager;
	}

	public int delete(String checkIds) {
		// TODO Auto-generated method stub
		for (String id : checkIds.split(", ")) {
			c_T_Sys_FileMapper.deleteByPrimaryKey(id);
		}
		return 1;
	}
	
	public int count(C_T_Sys_FileExample example,C_T_Sys_FileMapper c_T_Sys_FileMapper) {
		// TODO Auto-generated method stub
		return c_T_Sys_FileMapper.countByExample(example);
	}

	public Pager<C_T_Sys_File> selectLists(C_T_Sys_File c_T_Sys_File,Pager<C_T_Sys_File> pager, String projectUuid,String flag) {
			// TODO Auto-generated method stub
			C_T_Sys_FileExample example=new C_T_Sys_FileExample();
			Criteria criteria = example.createCriteria();
			if("1".equals(flag)){
				criteria.andAuxiliaryIdEqualTo(projectUuid);
			}else if("2".equals(flag) || "9".equals(flag)){
				criteria.andAuxiliaryIdEqualTo(projectUuid);
			}else{
				criteria.andUuidEqualTo(projectUuid);
				criteria.andAuxiliaryIdIsNull();
			}
			criteria.andIsDeleteEqualTo(0);
			example.or(criteria);
			int totalCount=new C_T_Sys_FileServiceImpl().count(example,c_T_Sys_FileMapper);
			pager.setTotalRecords(totalCount);
			example.setLimitStart(pager.getStartRecord());
			example.setLimitEnd(pager.getPageSize());
			example.setOrderByClause(pager.getOrderField()+" "+pager.getOrderDirection());
			// 取得总的记录条数
			List<C_T_Sys_File>  list=c_T_Sys_FileMapper.selectByExample(example);
			pager.setPageRecords(list);
			return pager;
	}

}
